<?php
class M_DbTable_Article extends Apps_Db_Table_Abstract {
	protected $_name = 'article';

	const REFERENCE_TYPE_TOUR 	= 1;
	const REFERENCE_TYPE_HOTEL 	= 2;
	const REFERENCE_TYPE_CRUISE = 3;
	const REFERENCE_TYPE_EVENT 	= 4;

	const REFERENCE_TYPE_NOMAL 	= 7;
	const REFERENCE_TYPE_ABOUT 	= 8;
	const REFERENCE_TYPE_VISA 	= 9;
	const REFERENCE_TYPE_COUNTRY 	= 12;
	const REFERENCE_TYPE_REGION 	= 13;

	const REFERENCE_TRAVEL_GUIDE  	= 15;
	const REFERENCE_BOOKING_GUIDE  	= 16;
	const REFERENCE_FAQS  			= 17;
	const REFERENCE_VCT_HISTORY  	= 18;
	const REFERENCE_WHY_VCT			= 19;
	const REFERENCE_CAREER			= 20;

	const STATUS_ACTIVE   = 1;
	const STATUS_INACTIVE = 0;
	const STATUS_DELETED  = -1;

	// Use for Country article
		const COUNTRY_ARTICLE_TYPE_GENERAL_INFORMATION  = 1;
		const COUNTRY_ARTICLE_TYPE_TRAVEL_DESTINATION	= 2;
		const COUNTRY_ARTICLE_TYPE_TRAVEL_NEWS			= 3;
		const COUNTRY_ARTICLE_TYPE_TRAVEL_TIPS 			= 4;

	// Use for Region article
		const REGION_ARTICLE_TYPE_TRAVEL_DESTINATION  = 2;


	public function getList() {
		$q = $this->_db
				  ->select()
				  ->from($this->_name)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_REGION)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_COUNTRY)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_CRUISE)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_EVENT)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_HOTEL)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_TOUR)
            	  ->where('article.reference_type !=?', self::REFERENCE_TYPE_VISA);
	
		return $q;
	}	
		
	//get article by reference id, type
	public function getArticleByReference ($referenceId, $referenceType, $columns = null) {
		return $this->_db
					->select()
					->from($this->_name, null)
					->where('reference_id =?', $referenceId)
					->where('reference_type =?', $referenceType)
					->columns(array('id','thumbnail'))
					->columns($columns);
	}
	public function getArticleById($articleId) {
		return $this->_db
					->select()
					->from($this->_name)
					->where('article.id = ?', $articleId);
	}

	//insert article by reference id, type
	public function addArticle($data, $referenceId, $referenceType, $keyFilter = null, $external = null) {
		$data['reference_id'] 	= $referenceId;
		$data['reference_type'] = $referenceType;

		$keyFilter = ($keyFilter != null)
					 ? $keyFilter
					 : array('title',
							 'en_title',
							 'overview',
							 'en_overview',
							 'detail',
							 'en_detail',
							 'description',
							 'reference_id',
							 'reference_type',
							 'thumbnail');

		return $this->safelyInsert($data, $keyFilter, $external);
	}

	//update article by reference id, type
	public function updateArticle($data, $referenceId, $referenceType, $keyFilter = null, $external = null) {
		$where['reference_id =?'] 	= $referenceId;
		$where['reference_type =?'] = $referenceType;

		$keyFilter = ($keyFilter != null)
					 ? $keyFilter
					 : array('title',
							 'en_title',
							 'overview',
							 'en_overview',
							 'detail',
							 'en_detail',
							 'description',
							 'thumbnail');

		return $this->safelyUpdate($data, $where, $keyFilter, $external);
	}

	//delete article by reference id, type
	public function deleteArticle($referenceId, $referenceType) {
		$article = $this->getArticleByReference($referenceId, $referenceType)->query()->fetch();
		if ($article) {
			if($this->safelyDelete(array('reference_id =?' => $referenceId, 'reference_type =?' => $referenceType))){
				unlink(Apps_Plugin_Upload_File::getPath($article['thumbnail']));
				return true;
			}
		}
	}
	//delete article by reference id, type
	public function deleteArticleById($articleId) {
		$article = $this->getArticleById($articleId)->query()->fetch();
		if ($article) {
			if($this->safelyDelete(array('id  = ?' => $articleId))){
				unlink(Apps_Plugin_Upload_File::getPath($article['thumbnail']));
				return true;
			}
		}
	}

	// List Country Article
	// Use for Country Article
	public function getListCountryArticle($type = null) {
		$q = $this->_db
				  ->select()
				  ->from('article', null)
				  ->join('country', 'country.id = article.reference_id', null)
				  ->where('reference_type = ?', self::REFERENCE_TYPE_COUNTRY)
				  ->columns(array(
				  			'id' 		 	=> 'article.id',
				  			'country_id' 	=> 'country.id',
				  			'type'		 	=> 'article.type',
				  			'description'	=> 'article.description',
				  			'created_date'	=> 'article.created_date'
				  		));

		if($type) {
			$q->where('type =?' , $type);
		}

		if($this->getLang() == 'en') {
			$q->columns(array(
							'title' 		=> 'article.en_title',
							'overview' 		=> 'article.en_overview',
							'country_name'	=> 'country.en_name'));
		} else {
			$q->columns(array(
							'title' => 'title',
							'overview' => 'overview',
							'country_name'	=> 'country.name'));
		}
		return $q;
	}

	// List Region Article
	// Use for Region Article
	public function getListRegionArticle($countryId = null, $type = null) {
		$q = $this->_db
				  ->select()
				  ->from('article', null)
				  ->join('region_destination', 'region_destination.id = article.reference_id', null)
				  ->join('country', 'country.id = region_destination.country_id', null)
				  ->where('reference_type = ?', self::REFERENCE_TYPE_REGION)
				  ->columns(array(
				  			'id' 		 	=> 'article.id',
				  			'region_destination_id' 	=> 'region_destination.id',
				  			'type'		 	=> 'article.type',
				  			'description'	=> 'article.description',
				  			'created_date'	=> 'article.created_date'
				  		));

		if($countryId) $q->where('country.id = ?', $countryId);
		if($type) {
			$q->where('type =?' , $type);
		}

		if($this->getLang() == 'en') {
			$q->columns(array(
							'title' 		=> 'article.en_title',
							'overview' 		=> 'article.en_overview',
							'region_destination_name'	=> 'region_destination.en_name',
							'country_name'	=> 'country.en_name'));
		} else {
			$q->columns(array(
							'title' => 'title',
							'overview' => 'overview',
							'region_destination_name'	=> 'region_destination.name',
							'country_name'	=> 'country.name'));
		}
		return $q;
	}

}